package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a05_删除字符串中的所有相邻重复项复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/16
 */
public class a05_删除字符串中的所有相邻重复项复习4 {
    public String removeDuplicates(String s) {
        char[] chars = s.toCharArray();

        Stack<Character> stack = new Stack<>();
        for (char c : chars) {
            if(!stack.isEmpty() && stack.peek().equals(c)){
                stack.pop();
            }else {
                stack.push(c);
            }
        }

        char[] cs = new char[stack.size()];
        for (int i = cs.length - 1; i >= 0; i--) {
            cs[i] = stack.pop();
        }
        return new String(cs);
    }
}
